import {UserActions} from './user';

const initialState = {
  current: 1,
  limit: 10,
  list: {},
  total: 0,
};

export const AlbumActions = {
  ALBUM_UPDATE: 'album_update',
  ALBUM_RESET: 'album_reset',
};

export function updateAlbum(payload) {
  return {
    type: AlbumActions.ALBUM_UPDATE,
    payload,
  };
}

export function resetAlbum() {
  return {
    type: AlbumActions.ALBUM_RESET,
  };
}

export default function album(state = initialState, action) {
  switch (action.type) {
    case AlbumActions.ALBUM_UPDATE:
      const list = {};
      if (action.payload.list) {
        action.payload.list.forEach(d => {
          list[d.id] = d;
        });
      }
      return {...state, ...action.payload, list: {...state.list, ...list}};
    case AlbumActions.ALBUM_RESET:
      return {};
    case UserActions.USER_SIGN_OUT:
      return {};
    default:
      return state;
  }
}
